﻿using WRJClient.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WRJClient.UI.Map;
using WRJClient.Utils;

namespace WRJClient.ViewModel
{
    public class InfomationViewModel : BaseViewModel<InfomationModel>, IMapObject
    {
        private BaseGraphic _gBody = new BaseGraphic();
        private BaseGraphic _gLabel = new BaseGraphic();
        public bool Visable
        {
            get { return _gBody.IsVisible || _gLabel.IsVisible; }
            set
            {
                _gBody.IsVisible = value;
                _gLabel.IsVisible = value;
            }
        }

        public InfomationViewModel(InfomationModel model = null) : base(model)
        {
            _gBody.IsVisible = false;
            _gLabel.IsVisible = false;


            InitGraphics();
        }

        public bool IsVisable
        {
            get { return _gBody.IsVisible; }
            set { if (_gBody.IsVisible != value) { _gBody.IsVisible = value; OnPropertyChanged(); }; }
        }
        public bool IsLabelVisable
        {
            get { return _gLabel.IsVisible; }
            set { if (_gLabel.IsVisible != value) { _gLabel.IsVisible = value; OnPropertyChanged(); }; }
        }
        public bool IsEnabled
        {
            get
            {
                try
                {
                    if (_gBody.Geometry != null)
                    {
                        return ArcgisUtils.IsExtentValid(_gBody.Geometry.Extent);
                    }
                }
                catch (Exception)
                {

                }
                return false;
            }
        }

        public void InitGraphics()
        {
            if (!string.IsNullOrEmpty(StaticData.Position))
            {
                char[] spliter = { ',' };
                string[] position = StaticData.Position.Split(spliter);
                if (position.Count() >= 2)
                {
                    if (double.TryParse(position.ElementAt(0), out double x) && double.TryParse(position.ElementAt(1), out double y))
                    {
                        string status = null;
                        if (StaticData.Type == 1)
                        {
                            status = "new";
                        }
                        else if (StaticData.Type == 2)
                        {
                            status = "destroyed";
                        }

                        GraphicFactory.CreateImageGraphic(_gBody, StaticData.DbId, GraphicPart.Base, SymbolHelper.Instance.GetPictureMarkerSymbolInfo(GetType(), "target", status), x, y);
                        GraphicFactory.CreateTextGraphic(_gLabel, StaticData.DbId, GraphicPart.Label, SymbolHelper.Instance.GetTextSymbolInfo(GetType()), StaticData.Name, x, y);
                    }
                }
            }
        }

        public void AddGraphics()
        {
            MapManager.Instance.AddGraphic("Layer3", _gBody);
            MapManager.Instance.AddGraphic("Layer4", _gLabel);
        }

        public void RemoveGraphics()
        {
            MapManager.Instance.RemoveGraphic("Layer3", _gBody);
            MapManager.Instance.RemoveGraphic("Layer4", _gLabel);
        }
    }
}
